#include "PauseHelp.h"
#include "Game.h"
#include "BitmapFont.h"

#include "../SGD Wrappers/SGD_String.h"

PauseHelp::PauseHelp(void)
{
	startItem = 0;
	endItem = HelpInfoManager::GetInstance()->getObjectVector().size();
	int numElements = Game::GetInstance()->GetScreenHeight() / 100;
	if(endItem > numElements - 1)
		endItem = numElements -1;
	maxItems = endItem;
	cursor = 0;
	pauseMenu = nullptr;
}


PauseHelp::~PauseHelp(void)
{

}


void PauseHelp::Update(float deltaTime)
{
	if(controllerDelay > 0.0f)
		controllerDelay -= deltaTime;

	if(dI->KeyPressed(DIK_UP) || dI->KeyPressed(DIK_W) || dI->JoystickDPadPressed(DIR_UP) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() < 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.17f;
		if(cursor > 0)
			cursor -= 1;
		else
			cursor = HelpInfoManager::GetInstance()->getObjectVector().size()-1;
	}
	else if(dI->KeyPressed(DIK_DOWN) || dI->KeyPressed(DIK_S) || dI->JoystickDPadPressed(DIR_DOWN) || (CSGD_DirectInput::GetInstance()->JoystickGetLStickYNormalized() > 0 && controllerDelay <= 0.0f))
	{
		controllerDelay = 0.25f;
		if((unsigned int)cursor < HelpInfoManager::GetInstance()->getObjectVector().size()-1)
			cursor+= 1;
		else
			cursor = 0;
	}

	if(maxItems >= 5)
	{
		if(cursor < startItem+1)
		{
			startItem = cursor;
			endItem = cursor + maxItems;
		}
		else if (cursor > endItem-1)
		{
			endItem = cursor+1;
			startItem = endItem - maxItems;
		}
	}
}

void PauseHelp::Render(void) const		
{
	//The rect that the user selects objects in. Displays the name of the object and an image.
	RECT selectionRect = {0,0,Game::GetInstance()->GetScreenWidth()/3+50,Game::GetInstance()->GetScreenHeight()};
	//The rect that displays the description of the currently selected object.
	RECT descriptionRect = {Game::GetInstance()->GetScreenWidth()/3+50,0,Game::GetInstance()->GetScreenWidth(),Game::GetInstance()->GetScreenHeight()};

	D3D->DrawRect(selectionRect, D3DCOLOR_ARGB(255,30,30,40));
	D3D->DrawRect(descriptionRect, D3DCOLOR_ARGB(255,50,40,90));
	for(int curItem = startItem; curItem < endItem; ++curItem)
	{
		RECT renderRect;
		ostringstream infoStream;
		renderRect.top = 10 + 100*(curItem-startItem) + 20*(curItem-startItem);
		renderRect.bottom = 100 + 100*(curItem-startItem)+ 20*(curItem-startItem);
		renderRect.left = selectionRect.left;
		renderRect.right = selectionRect.right;
		D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,30,40,70));

		vector<HelpInfo*> infoVector = (HelpInfoManager::GetInstance()->getObjectVector());
		infoStream << infoVector[curItem]->getName().c_str() << "      " << curItem;
		BitmapFont::GetInstance()->Print(infoStream.str().c_str(), (float)renderRect.left, (float)renderRect.top ,0, D3DCOLOR_ARGB(255,0,0,0));
		infoStream.str("");

		renderRect.bottom-=10;
		renderRect.right-= 10;
		renderRect.left = renderRect.right - 64;
		renderRect.top = renderRect.bottom - 64;
		D3D->DrawRect(renderRect,D3DCOLOR_ARGB(255,100,50,50));
		if(infoVector[curItem]->getImageID() != -1)
		{
			tM->Draw(infoVector[curItem]->getImageID(),renderRect.left,renderRect.top);
		}
	}


	RECT selectedRect;
	selectedRect.top = 10 + 100*	(cursor-startItem) + 20*(cursor-startItem);
	selectedRect.bottom = 100 + 100*(cursor-startItem) + 20*(cursor-startItem);
	selectedRect.left = selectionRect.left;
	selectedRect.right = selectionRect.right-4;

	D3D->DrawHollowRect(selectedRect,D3DCOLOR_ARGB(255,0,0,255));
	ostringstream descpStream;
	if(startItem != endItem)
		descpStream << HelpInfoManager::GetInstance()->getObjectVector()[cursor]->getDescription().c_str();
	BitmapFont::GetInstance()->Print(descpStream.str().c_str(), (float)descriptionRect.left+5, (float)descriptionRect.top,0, descriptionRect.right-descriptionRect.left, D3DCOLOR_ARGB(255,0,0,0));

	

	D3D->DrawLine(Game::GetInstance()->GetScreenWidth()/3+50,0,Game::GetInstance()->GetScreenWidth()/3+50,Game::GetInstance()->GetScreenHeight(),
	D3DCOLOR_ARGB(255,0,0,0),6);
}

void PauseHelp::Enter(void)		
{
	GameState::Enter();

	startItem = 0;
	endItem = HelpInfoManager::GetInstance()->getObjectVector().size();
	if(endItem > 5)
		endItem = 5;
	maxItems = endItem;
	cursor = 0;
	pauseMenu = nullptr;
}

void PauseHelp::Enter(Pause* pause)
{
	GameState::Enter();
	pauseMenu = pause;

	startItem = 0;
	endItem = HelpInfoManager::GetInstance()->getObjectVector().size();
	int numElements = Game::GetInstance()->GetScreenHeight() / 100;
	if(endItem > numElements - 1)
		endItem = numElements -1;
	maxItems = endItem;
	cursor = 0;
	pauseMenu = nullptr;
}
	
void PauseHelp::Exit(void)		
{
	GameState::Exit();
}

